from ursina import * 
from ursina.prefabs.first_person_controller import FirstPersonController



app = Ursina()

grass_texture = load_texture('assets/block.jpg')
stone_texture = load_texture('assets/block2.jpg')
sky_texture = load_texture('assets/skybox.png')
arm_texture = load_texture('assets/arm_texture.png')
punch_sound = Audio('assets/punch_sound',loop=False,autoplay=False)
block_pick = 1



#柏林噪声
from perlin_noise import PerlinNoise

class Block(Button):
    def __init__(self,position=(0,0,0),texture = grass_texture):
        super().__init__(
            parent = scene,
            position = position,
            model = 'assets/block',
            origin_y = 0.5,
            texture = texture,
            color = color.color(0,0,random.uniform(0.9,1)),
            highlight_color = color.green,
            scale = 0.5
        )

    def input(self,key):
        if self.hovered:
            if key == 'left mouse down':
                punch_sound.play()
                destroy(self)
            if key == 'right mouse down':
                        punch_sound.play()
                        block = Block(position = self.position+mouse.normal,texture=grass_texture)

class Sky(Entity):
     def __init__(self):
          super().__init__(
               parent = scene,
               model = 'sphere',
               texture = sky_texture,
               scale = 150,
               double_sided = True
          )

class Hand(Entity):
     def __init__(self):
          super().__init__(
               parent = camera.ui,
               model = 'assets/arm',
               texture = arm_texture,
               scale = 0.2,
               rotation = Vec3(150,-10,0),
               position = Vec2(0.4,-0.6)
          )

def active(self):
    self.position = Vec2(0.3,-0.5)

def passive(self):
    self.position = Vec2(0.4,-0.6)

noise = PerlinNoise(octaves = 3, seed = 20230914)
scale = 24

for z in range(40):
    for x in range(40):
        block = Block(position = (x,0,z))
        block.y = floor(noise([x/scale,z/scale])*8)

player = FirstPersonController()
sky = Sky()
hand = Hand()

app.run()